package com.yykj.hospital.server.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yykj.common.enums.HospitalCheckType;
import com.yykj.common.vo.ResultVO;
import com.yykj.hospital.server.dto.HospitalCheckImportDTO;
import com.yykj.hospital.server.form.HospitalCheckForm;
import com.yykj.hospital.server.handler.HospitalCheckHandler;
import com.yykj.hospital.server.model.entity.HospitalCheck;
import com.yykj.hospital.server.service.HospitalCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/backstage/check")
@SuppressWarnings("unchecked")
@Api(tags = "检查检验管理")
public class HospitalCheckController {
    @GetMapping("/list")
    @ApiOperation("搜索列表")
    public ResultVO<List<HospitalCheck>> listHospitalChek(@RequestParam Integer hospitalId,
                                                          @RequestParam(required = false) Integer depId,
                                                          @RequestParam HospitalCheckType type,
                                                          @RequestParam(required = false) String keyword) {
        HospitalCheckForm build = HospitalCheckForm.builder()
                .hospitalId(hospitalId)
                .depId(depId)
                .type(type)
                .keyword(keyword)
                .build();

        return ResultVO.success(hospitalCheckService.listHospitalCheck(build));
    }

    @PostMapping("/add")
    @ApiOperation("添加")
    @ApiOperationSupport(ignoreParameters = {"id"})
    public ResultVO<HospitalCheck> addHospitalCheck(@RequestBody  HospitalCheck check){
        return ResultVO.success(hospitalCheckService.editHospitalCheck(check));
    }

    @PutMapping("/edit")
    @ApiOperation("修改")
    public ResultVO<HospitalCheck> editHospitalCheck(@RequestBody HospitalCheck check){
        return ResultVO.success(hospitalCheckService.editHospitalCheck(check));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除")
    public ResultVO<HospitalCheck> deleteHospitalCheck(@PathVariable("id")Integer id){
        hospitalCheckService.deleteHospitalCheck(id);
        return ResultVO.success();
    }


    @PostMapping("/upload")
    @ApiOperation("导入检查检验")
    public ResultVO importHospitalCheck(Integer hospitalId, MultipartFile file){
        try {
            HospitalCheckHandler handler = new HospitalCheckHandler();
            ImportParams params = new ImportParams();
            params.setVerifyHandler(handler);
            params.setNeedVerify(true);
            ExcelImportResult<HospitalCheckImportDTO> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
                    HospitalCheckImportDTO.class, params);
            if (result.isVerifyFail()) {
                return ResultVO.error(result.getFailList());
            } else {
                boolean b = hospitalCheckService.checkHospitalCheckList(hospitalId,result);
                if(b){
                    hospitalCheckService.importCheckList(hospitalId,result.getList());
                    return ResultVO.success(result.getList());
                }else{
                    return ResultVO.error(result.getFailList());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("请按照模板正确填写");
        }
    }

    private HospitalCheckService hospitalCheckService;

    @Autowired
    public void setHospitalCheckService(HospitalCheckService hospitalCheckService) {
        this.hospitalCheckService = hospitalCheckService;
    }
}
